package cn.xshi.monitor.service.impl;

import cn.xshi.common.base.BaseService;
import cn.xshi.common.idgeneration.UUID;
import cn.xshi.common.util.ExceptionUtil;
import cn.xshi.common.util.StringUtil;
import cn.xshi.monitor.dao.GraphPelDao;
import cn.xshi.monitor.dao.GraphPelElementDao;
import cn.xshi.monitor.model.GraphPel;
import cn.xshi.monitor.model.GraphPelElement;
import cn.xshi.monitor.service.GraphPelService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * @Desc 图元管理
 * @Author 邓纯杰
 * @CreateTime 2012-12-12 12:12:12
 */
@Service
public class GraphPelServiceImpl extends BaseService implements GraphPelService {

    @Resource
    GraphPelDao graphPelDao;

    @Resource
    GraphPelElementDao graphPelElementDao;

    /**
     * 分页查询
     * @param condition
     * @return
     */
    public List<GraphPel> getGraphPelListByCondition(Map<String,Object> condition){
        return graphPelDao.getGraphPelListByCondition(condition);
    }

    /**
     * 查询单条记录
     * @param graphPelId
     * @return
     */
    public GraphPel getGraphPelById(String graphPelId){
        Map<String,Object> condition = new HashMap<>();
        condition.put("graphPelId",graphPelId);
        GraphPel graphPel = graphPelDao.getGraphPelById(graphPelId);
        graphPel.setGraphPelElements(graphPelElementDao.getGraphPelElementListByCondition(condition));
        return graphPel;
    }

    /**
     * 添加
     * @param graphPel
     * @return
     */
    public int addGraphPel(GraphPel graphPel){
        int i = 0;
        try{
            String userId = getCurrentAccountId();
            Date date = getDate();
            graphPel.setId(toUUID());
            graphPel.setCreateBy(userId);
            graphPel.setCreateTime(date);
            i = graphPelDao.addGraphPel(graphPel);
            if(null != graphPel.getGraphPelElements() && !graphPel.getGraphPelElements().isEmpty()){
                for(GraphPelElement graphPelElement:graphPel.getGraphPelElements()){
                    graphPelElement.setGraphPelId(graphPel.getId());
                    graphPelElement.setId(toUUID());
                    graphPelElement.setCreateBy(userId);
                    graphPelElement.setCreateTime(date);
                    graphPelElementDao.addGraphPelElement(graphPelElement);
                }
            }
        }catch (Exception e){
            throw new ExceptionUtil(e.getMessage(),e.getCause());
        }

        return i;
    }

    /**
     * 修改
     * @param graphPel
     * @return
     */
    public int updateGraphPel(GraphPel graphPel){
        int i = 0;
        try{
            String userId = getCurrentAccountId();
            Date date = getDate();
            graphPel.setUpdateId(userId);
            graphPel.setUpdateTime(date);
            i = graphPelDao.updateGraphPel(graphPel);
            if(!StringUtil.isEmpty(graphPel.getId())){
                Map<String,Object> condition = new HashMap<>();
                condition.put("graphPelId",graphPel.getId().split(","));
                graphPelElementDao.delGraphPelElements(condition);
            }
            if(null != graphPel.getGraphPelElements() && !graphPel.getGraphPelElements().isEmpty()){
                for(GraphPelElement graphPelElement:graphPel.getGraphPelElements()){
                    graphPelElement.setGraphPelId(graphPel.getId());
                    graphPelElement.setCreateBy(userId);
                    graphPelElement.setCreateTime(date);
                    graphPelElement.setId(UUID.toUUID());
                    graphPelElementDao.addGraphPelElement(graphPelElement);
                }
            }
        }catch (Exception e){
            throw new ExceptionUtil(e.getMessage(),e.getCause());
        }
        return i;
    }

    /**
     * 删除
     * @param condition
     * @return
     */
    public int delGraphPel(Map<String,Object> condition){
        int i = 0;
        try{
            i = graphPelDao.delGraphPel(condition);
            graphPelElementDao.delGraphPelElements(condition);
        }catch (Exception e){
            throw new ExceptionUtil(e.getMessage(),e.getCause());
        }
        return i;
    }
}
